package com.tanhua.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.BlackListMapper;
import com.tanhua.common.pojo.BlackList;
import com.tanhua.server.service.BlackListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BlackListServiceImpl implements BlackListService {

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 查看黑名单
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<BlackList> queryBlackList(Long userId, Integer page, Integer pageSize) {
        Page<BlackList> pager = new Page<>(page, pageSize);
        QueryWrapper<BlackList> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.orderByDesc("created");
        IPage<BlackList> blackListIPage = this.blackListMapper.selectPage(pager, query);
        return blackListIPage;
    }

    /**
     * 移除黑名单
     *
     * @param id
     * @param userId
     */
    @Override
    public Boolean removeBlackList(Long id, Long userId) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", id);
        queryWrapper.eq("blackUserId", userId);
        return this.blackListMapper.delete(queryWrapper) > 0;
    }
}
